﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_Modes;
using Zor_IDAL;
using Zor_Tools;
using System.Data;
namespace Zor_DAL
{
    public class Zor_TenantEnterprise_DAL<T> where T : class
    {
        #region -租户管理 -
        /// <summary>
        /// 获取个人或企业管理列表（不分页）
        /// create zhl by 2017.4.1     
        /// </summary>
        /// <returns></returns>
        public static List<T> SelectViewTenant(string where = null)
        {
            string strwhere = "";
            if (!string.IsNullOrEmpty(where))
            {
                strwhere = "1=1 " + where;
            }
            var sql = "select * from [dbo].[V_Zor_Tenant] where " + strwhere + " order by Tenant_Id";
            return DapperQuery<T>.DoQueryList(sql, null);
        }

        /// <summary>
        /// 查询所有行业
        /// create zhl by 2017.3.24
        /// <param name="Category">所属类别（1职业、2人口性质、3国籍4、行业）</param>
        /// </summary>
        /// <returns></returns>
        public static List<T> SelectIndustry(int category)
        {
            var sql = "select [CommonType_ID],[CommonType_Name] from [Zor_CommonType] where Category=@category";
            return DapperQuery<T>.DoQueryList(sql, new { Category = category });
        }

        /// <summary>
        /// 根据Id查询数据
        /// create zhl by 17-3-19
        /// </summary>
        /// <param name="tenant_Id">租户id</param>
        /// <returns></returns>
        public static List<T> SelectPageListId(string tenant_Id)
        {
            var sql = "select * from [V_Zor_Tenant] where Tenant_Id=@tenant_Id";
            return DapperQuery<T>.DoQueryList(sql, new { Tenant_Id = tenant_Id });
        }
        /// <summary>
        /// 租户管理-新增
        /// create zhl by 17-3-19
        /// </summary>
        /// <param name="tenant">租户对象</param>
        /// <returns>执行结果：1 成功 0失败</returns>
        public static BaseAnswer InsertTenant(Zor_Tenant tenant)
        {

            var sql = @"if not exists(select Tenant_Code from [Zor_Tenant] where Tenant_IDNumber  =@Tenant_IDNumber   and  Tenant_Type=@Tenant_Type) 
                        insert into Zor_Tenant values(@Tenant_Name,@Tenant_ProprietorPhone,@Tenant_IDNumber,@Tenant_Sex,@Tenant_Company,@Tenant_Vocation,@Tenant_TaxRegistrationNum,@Tenant_IsTaxRegistration,@Tenant_LegalPerson,@Tenant_Phone,@Tenant_TaxAuthority,@Tenant_Number,@Tenant_Nature,@Tenant_Nationality,@Ept_Industry,@Tenant_Code,@Tenant_CreateTime,@Tenant_CreateName,@Tenant_ScopeBusiness,@Tenant_Type,@Tenant_Note,@Dt_Id)";
            BaseAnswer bar = DapperInsert<T>.DoInsert(sql, tenant);
            return bar;
        }

        /// <summary>
        /// 租户管理-修改
        /// create zhl by 17-3-19
        /// </summary>
        /// <param name="tenant">租户对象</param>
        /// <returns>执行结果：1 成功 0失败</returns>
        public static BaseAnswer UpdateTenant(Zor_Tenant tenant)
        {
            var sql = "";
            if (tenant.Tenant_Name != null)
                sql += "if  exists(select Tenant_Name from [Zor_Tenant] where Tenant_Name =@Tenant_Name ) ";
            sql += "update [Zor_Tenant] set Tenant_Name=@Tenant_Name,Tenant_ProprietorPhone=@Tenant_ProprietorPhone,Tenant_IDNumber=@Tenant_IDNumber,Tenant_Sex=@Tenant_Sex,Tenant_Company=@Tenant_Company,Tenant_Vocation=@Tenant_Vocation,Tenant_TaxRegistrationNum=@Tenant_TaxRegistrationNum,Tenant_IsTaxRegistration=@Tenant_IsTaxRegistration,Tenant_LegalPerson=@Tenant_LegalPerson,Tenant_Phone=@Tenant_Phone,Tenant_TaxAuthority=@Tenant_TaxAuthority, ";
            sql += "Tenant_Number=@Tenant_Number,Tenant_Nature=@Tenant_Nature,Tenant_Nationality=@Tenant_Nationality,Ept_Industry=@Ept_Industry,Tenant_Code=@Tenant_Code,Tenant_ScopeBusiness=@Tenant_ScopeBusiness,Tenant_Note=@Tenant_Note where Tenant_Id = @Tenant_Id and Tenant_Type=@Tenant_Type";
            BaseAnswer bar = DapperEdit<T>.DoEdit(sql, tenant);
            return bar;
        }
        /// <summary>
        /// 删除租户
        /// create zhl by 17-3-19
        /// </summary>
        /// <param name="id">删除租户的id</param>
        /// <returns>1正常 返回0</returns>
        public static BaseAnswer DeleteTenant(int[] id)
        {
            var sql = "";
            sql += "if not exists(select * from [Zor_Lease] where Tenant_Id =@Tenant_Id ) ";
            sql += "delete from  [Zor_Tenant]   where   Tenant_Id in @Tenant_Id";
            BaseAnswer bar = DapperDelete<T>.DoDelete(sql, new { Tenant_Id = id.ToArray<int>() });
            return bar;
        }
        #endregion
    }
}
